package com.gqb.lucene.PageQuery;

import java.io.IOException;
import java.nio.file.Paths;
import java.util.Map;

import org.apache.lucene.analysis.cn.smart.SmartChineseAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.Term;
import org.apache.lucene.queryparser.classic.MultiFieldQueryParser;
import org.apache.lucene.queryparser.classic.ParseException;
import org.apache.lucene.queryparser.classic.QueryParser;
import org.apache.lucene.search.BooleanClause;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TermQuery;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;

public class Searcher {
	private Directory indexDir;
	private IndexReader reader;
	private IndexSearcher is;

	public Searcher(String path) throws IOException {
		indexDir = FSDirectory.open(Paths.get(path));
		reader = DirectoryReader.open(indexDir);
		is = new IndexSearcher(reader);
	}
	
	public void reopen(IndexWriter indexWriter) throws IOException{
		IndexReader newreader=DirectoryReader.openIfChanged((DirectoryReader)reader);
//		IndexReader newreader=DirectoryReader.openIfChanged((DirectoryReader)reader,indexWriter,true); 
		if(newreader!=null){
			reader=newreader;
			is = new IndexSearcher(reader);
		}
	}
	public Document getDocument(ScoreDoc scoreDoc) throws IOException{
		
		return is.doc(scoreDoc.doc);
	}
	/**
	 * 对 索引指定的字段 fileName 进行
	 * 
	 * @param fileName
	 * @return
	 * @throws IOException
	 */
	public TopDocs TermQueryL(String fieldName, String fileName)
			throws IOException {
		Term term = new Term(fieldName, fileName);
		
		TermQuery termQuery = new TermQuery(term);
		TopDocs hits = is.search(termQuery, 10);
		return hits;
	}

	/**
	 * 还需要重构， 对多域进行搜索，可以指定and 和or以及字段
	 * 
	 * @param termMaps
	 * @return
	 * @throws ParseException
	 * @throws IOException
	 */
	public TopDocs TermsQueryL(Map<String, String> termMaps)
			throws ParseException, IOException {
		String[] fields = new String[termMaps.size()];
		String[] params = new String[termMaps.size()];
		BooleanClause.Occur[] flags = new BooleanClause.Occur[termMaps.size()];
		int i = 0;
		for (String field : termMaps.keySet()) {
			fields[i] = field;
			params[i] = termMaps.get(field);
			flags[i] = BooleanClause.Occur.MUST;
			i++;
		}
		SmartChineseAnalyzer analyzer = new SmartChineseAnalyzer();
		Query query = MultiFieldQueryParser.parse(params, fields, flags,
				analyzer);
		TopDocs hits = is.search(query, 10);
		return hits;
	}

	public TopDocs queryParse(String fieldName, String text)
			throws IOException, ParseException {
		SmartChineseAnalyzer analyzer = new SmartChineseAnalyzer();
		QueryParser parser = new QueryParser(fieldName, analyzer);
		Query query = parser.parse(text);
		TopDocs hits = is.search(query, 10);
		return hits;
	}

	
}
